package com.quectel.cms.controller.thirdconfig;


import com.quectel.cms.controller.BaseController;
import com.quectel.cms.util.SessionHolder;
import com.quectel.cms.vo.thirdconfig.ThirdPlatformConfigVo;
import com.quectel.constant.core.thirdplatformconfig.ThirdPlatformConfigConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.thirdconfig.dto.ThirdPlatformConfigDto;
import com.quectel.core.module.thirdconfig.service.ThirdPlatformConfigService;
import com.quectel.util.common.JacksonUtils;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/05/09 10:59
 */
@RestController
@RequestMapping("thirdPlatformConfig")
@Tag(name = "ThirdPlatformConfigController", description = "第三方配置相关api")
public class ThirdPlatformConfigController extends BaseController {

    @DubboReference
    private ThirdPlatformConfigService thirdPlatformConfigService;


    /**
     * 根据类型查询第三方配置单条记录
     */
    @GetMapping("infoForEdit/{type}")
    @RequirePermissions(values = "thirdPlatformConfig:edit")
    @Operation(summary = "根据类型查询第三方配置单条记录")
    public Response<ThirdPlatformConfigDto> infoForEdit(@PathVariable("type") Integer type) {


        ThirdPlatformConfigDto thirdPlatformConfigDto = thirdPlatformConfigService.selectByTenantAndType(SessionHolder.getSysUser().getTenantId(), type);

        return Response.<ThirdPlatformConfigDto>ok().wrap(thirdPlatformConfigDto);
    }


    /**
     * 保存第三方配置单条记录
     */
    @PostMapping("save")
    @RequirePermissions(values = "thirdPlatformConfig:edit")
    @Operation(summary = "保存第三方配置单条记录")
    public Response<Object> save(@RequestBody @Validated ThirdPlatformConfigVo vo) {
        ThirdPlatformConfigDto dbValue =
                thirdPlatformConfigService.selectByTenantAndType(SessionHolder.getSysUser().getTenantId(), vo.getType());
        if (dbValue != null) {
            thirdPlatformConfigService.deleteById(dbValue.getId());
        }
        //填充基础数据
        ThirdPlatformConfigDto thirdPlatformConfigDto = new ThirdPlatformConfigDto();
        fillSaveCommonData(thirdPlatformConfigDto);

        thirdPlatformConfigDto.setType(vo.getType());

        if (ThirdPlatformConfigConstants.PlatformType.SMS_CONFIG.getType().equals(thirdPlatformConfigDto.getType())) {
            thirdPlatformConfigDto.setConfigJson(JacksonUtils.toJsonString(vo.getValues().getSms()));
        } else if (ThirdPlatformConfigConstants.PlatformType.WECHAT_OFFICIAL_CONFIG.getType().equals(thirdPlatformConfigDto.getType())) {
            thirdPlatformConfigDto.setConfigJson(JacksonUtils.toJsonString(vo.getValues().getWechatOfficial()));
        }
        else if (ThirdPlatformConfigConstants.PlatformType.BAIDU_BCE_CONFIG.getType().equals(thirdPlatformConfigDto.getType())) {
            thirdPlatformConfigDto.setConfigJson(JacksonUtils.toJsonString(vo.getValues().getBaidubce()));
        }
        thirdPlatformConfigService.save(thirdPlatformConfigDto);
        return Response.ok();
    }

}
